﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using MvcEngine.Core;
using MvcEngine.Core.Extensions;
using MvcEngine.Core.Utils;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core.Helpers;
using MvcEngine.Mvc.ModelBinders;
using MvcEngine.Core.Base;
using MvcEngine.Contact.Models;

namespace MvcEngine.Contact.ModelBinders
{
    public class SettingsModelBinder : ModelBinderBase
    {
        public override object Bind(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request = controllerContext.HttpContext.Request;
            ContactSettings settings = IoC.Resolve<ContactSettings>();
            //Add everything to ModelState
            var modelState = controllerContext.Controller.ViewData.ModelState;

            settings.SendTo = request.Form.Get("SendTo");
            settings.Email = request.Form.Get("Email");
            settings.EmailSubjectPrefix = request.Form.Get("EmailSubjectPrefix");
            settings.SmtpPassword = request.Form.Get("SmtpPassword");
            settings.SmtpServer = request.Form.Get("SmtpServer");
            settings.SmtpServerPort = request.Form.Get("SmtpServerPort").ToInt();
            settings.SmtpUserName = request.Form.Get("SmtpUserName");

            Validate(settings, controllerContext);
            return settings;
        }
    }
}
